package top.hunfan.kindle.jikeshijian.document;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import top.hunfan.kindle.domain.Book;
import top.hunfan.kindle.domain.ChapterInfo;
import top.hunfan.kindle.writer.MobiWriter;
import top.hunfan.kindle.writer.Writer;

public class CrawlDoc {

    private static final ExecutorService crawlerService = Executors.newFixedThreadPool(5);

    private static RestTemplate restTemplate = new RestTemplate();

    public static HttpHeaders getHeaders(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        headers.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
        headers.add("Referer", "https://time.geekbang.org/column/article/0?cid=81");
        headers.add("Cookie", "XXXX");
        headers.add("Accept", "application/json, text/plain, */*");
        return headers;
    }

    public static void main(String[] args) throws IOException {
        HttpHeaders headers = getHeaders();
        Map<String, Object> body = new HashMap<>();
        body.put("cid", "48");
        body.put("size", 200);
        body.put("prev", 0);
        body.put("order", "earliest");
        body.put("sample", false);
        HttpEntity<String> requestEntity = new HttpEntity<>(JSON.toJSONString(body), headers);
        JSONObject object = restTemplate.postForObject("https://time.geekbang.org/serv/v1/column/articles", requestEntity, JSONObject.class);
        JSONArray arr = object.getJSONObject("data").getJSONArray("list");
        List<Integer> ids = new LinkedList<>();
        for(int i=0;i < arr.size();i++){
            ids.add(arr.getJSONObject(i).getInteger("id"));
        }
        List<CompletableFuture<ChapterInfo>> tasks = ids.stream()
                .map(id -> CompletableFuture.supplyAsync(() -> getChapterInfo(id), crawlerService))
                .collect(Collectors.toList());
        List<ChapterInfo> result = tasks.stream().map(CompletableFuture::join)
                .collect(Collectors.toList());

        System.out.println("all finish,size:" + result.size());

        Book book = new Book();
        book.setAuthor("陈皓");
        book.setName("左耳听风");
        book.setId("123456");
        book.setChapters(result.toArray(new ChapterInfo[result.size()]));
        URL coverUrl = new URL("https://static001.geekbang.org/resource/image/5f/cd/5f197321896070a8ba9635b5b03764cd.jpg");
        Writer writer = new MobiWriter(coverUrl);
        writer.generate(book, "D:\\tmp7");
        System.out.println("执行结束！");
    }

    private static final String BAST_URL = "https://time.geekbang.org/serv/v1/article";

    public static ChapterInfo getChapterInfo(Integer id){
        Map<String, Object> body = new HashMap<>();
        body.put("id", id);
        body.put("include_neighbors", false);
        HttpHeaders headers = getHeaders();
        HttpEntity<String> requestEntity = new HttpEntity<>(JSON.toJSONString(body), headers);
        JSONObject object = restTemplate.postForObject(BAST_URL, requestEntity, JSONObject.class);
        JSONObject data = object.getJSONObject("data");
        ChapterInfo info = new ChapterInfo();
        info.setTitle(data.getString("article_title"));
        info.setBody(data.getString("article_content"));
        return info;
    }

}

